local addonName, IWonew = ...

local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("TRADE_SKILL_ITEM_CRAFTED_RESULT")

frame:SetScript("OnEvent", function(self, event, d1, d2)
  if event == "TRADE_SKILL_ITEM_CRAFTED_RESULT" then
    if IWonew.Temp_Vars.needCraftLog then
      local currentRole, currentRealm = UnitFullName("player")
      local resultString = "role=" .. currentRole .. "&realm=" ..currentRealm .. "&itemID=" .. d1.itemID
      -- 产能
      local multicraf = (d1.multicraft and d1.multicraft > 0 and 1) or 0
      resultString = resultString .. "&multicraf=" .. multicraf .. "&quantity=" .. d1.quantity
      -- 奇思
      local hasIngenuityProc = d1.hasIngenuityProc and 1 or 0
      resultString = resultString .. "&hasIngenuityProc=" .. hasIngenuityProc .. "&concentrationSpent=" .. d1.concentrationSpent .. "&ingenuityRefund=" .. d1.ingenuityRefund
      -- 充裕
      if d1.resourcesReturned then
        resultString = resultString .. "&resourcesReturned=1"
        for i = 1, #d1.resourcesReturned do
          resultString = resultString .. "&resourcesReturned_" .. d1.resourcesReturned[i].itemID .. "=" .. d1.resourcesReturned[i].quantity
        end
      end
      IWonew.Log("TRADE_SKILL_ITEM_CRAFTED_RESULT----" .. "{" .. resultString .. "}")
    end
    
  end
  if event == "PLAYER_LOGIN" then
    C_Timer.After(3, function()
      print("玩家已登录，所有功能就绪！1")
      print("延迟 10 秒后触发")
      IWonew.CreateTextFrame(80, "登录", 0, 0)
      -- IWonew.CreateTextFrame(80, "点火", 80, 0)
      -- IWonew.CreateTextFrame(80, "奇思", 160, 0)
      -- IWonew.CreateTextFrame(80, "锻A", 240, 0)
      -- IWonew.CreateTextFrame(80, "附B", 320, 0)
      -- IWonew.CreateTextFrame(80, "锻C", 400, 0)
      -- IWonew.CreateTextFrame(80, "附D", 480, 0)
      -- IWonew.CreateTextFrame(80, "拆E", 560, 0)
      -- IWonew.CreateTextFrame(80, "匠F", 640, 0)
      -- IWonew.CreateTextFrame(80, "点A", 720, 0)
    end)
  end
end)



hooksecurefunc(C_TradeSkillUI, "CraftEnchant",
		function(recipeID, amount, craftingReagentInfoTbl, enchantItemLocation, concentrating)
      -- print("CraftEnchant---recipeID", recipeID)
      -- print("###", #craftingReagentInfoTbl)
      -- print(IWonew.TableToString(craftingReagentInfoTbl))
		end
)

hooksecurefunc(C_TradeSkillUI, "CraftRecipe",
  function(recipeID, amount, craftingReagentInfoTbl, recipeLevel, orderID, concentrating)
    -- local recipeInfo = C_TradeSkillUI.GetRecipeInfo(recipeID)

    -- IWonew.Log("recipeInfo----START")
    -- if recipeInfo then
    --   IWonew.Log("recipeInfo==" .. recipeInfo.name .. ",---" .. recipeID)
    -- end
    -- for i = 1, #craftingReagentInfoTbl do
    --   local itemName = C_Item.GetItemInfo(craftingReagentInfoTbl[i].itemID)
    --   -- IWonew.Log("{itemName = " .. itemName .. ", slotIndex=" .. craftingReagentInfoTbl[i].dataSlotIndex .. ",quantity=" .. craftingReagentInfoTbl[i].quantity .. "}")
    --   IWonew.Log("{itemName = " .. itemName .. ", quantity = " .. craftingReagentInfoTbl[i].quantity .. ", needSlot = true, dataSlotIndex = " .. craftingReagentInfoTbl[i].dataSlotIndex .. "}")
    -- end

    -- IWonew.Log("recipeInfo----END")
    
    -- print("CraftRecipe---recipeID", recipeID)
    -- print("CraftRecipe---orderID", orderID)
    -- print("###", #craftingReagentInfoTbl)
    -- print(IWonew.TableToString(craftingReagentInfoTbl))
  end
)

hooksecurefunc(C_TradeSkillUI, "CraftSalvage",
  function(recipeID, amount, itemTargetLocation, craftingReagentTbl, applyConcentration)
    -- print("CraftSalvage---recipeID", recipeID)
    -- print("CraftSalvage---itemTargetLocation", itemTargetLocation)
    -- print(IWonew.TableToString(craftingReagentTbl))
  end
)